/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "quakedef.h"
#include "menu.shared.h"


int 	lanConfig_port;
char	lanConfig_portname[6];
char	lanConfig_joinname[22];

void M_ConfigureNetSubsystem (void)
{
	// enable/disable net systems to match desired config
	Cbuf_AddText ("stopdemo\n");
	net_hostport = lanConfig_port;
}


class qmenu_lanconfig_t : public qmenu_t
{
protected:
	class qmenu_lanconfig_submenu_t : public qmenu_option_t
	{
	public:
		qmenu_lanconfig_submenu_t (char *text, m_state_t submenu)
		{
			Q_strncpy (this->Text, text, sizeof (this->Text) - 1);
			this->Submenu = submenu;
			this->Active = TRUE;
		}

		int Draw (int y)
		{
			M_DrawCommand (y, this->Text);
			return y + 10;
		}

		BOOL Key (int key)
		{
			if (key == K_ENTER)
			{
				M_ConfigureNetSubsystem ();
				menus[this->Submenu]->EnterFunc ();
				return TRUE;
			}
			else return FALSE;
		}

	private:
		m_state_t Submenu;
	};

public:
	void EnterFunc (void)
	{
		if (!tcpipAvailable)
			return;

		this->EnterMenu ();
		this->SetupLanConfig ();
	}

protected:
	void AddCommonOptions (char *type)
	{
		static char *protocol = "TCP/IP";

		this->AddOption (new qmenu_banner_t ("gfx/p_multi.lmp"));
		this->AddOption (new qmenu_divider_t ());

		this->AddOption (new qmenu_textlabel_t (type, protocol));
		this->AddOption (new qmenu_textlabel_t ("Address", my_tcpip_address));
		this->AddOption (new qmenu_divider_t ());

		this->AddOption (new qmenu_numericbox_t ("Port", lanConfig_portname, 18));
		this->AddOption (new qmenu_divider_t ());
	}

	void SetupLanConfig (void)
	{
		lanConfig_port = DEFAULTnet_hostport;
		sprintf (lanConfig_portname, "%u", lanConfig_port);

		m_return_onerror = FALSE;
		m_return_reason[0] = 0;
	}

	void CustomDraw (void)
	{
		// fixup the port so that it stays within range
		int l = Q_atoi (lanConfig_portname);

		if (l > 65535)
			l = lanConfig_port;
		else lanConfig_port = l;

		sprintf (lanConfig_portname, "%u", lanConfig_port);

		// print the return reason if there was one
		if (m_return_reason[0]) M_PrintWhite (160 - strlen (m_return_reason) * 4, 148, m_return_reason);
	}
};


class qmenu_joingame_t : public qmenu_lanconfig_t
{
private:
	class qmenu_jg_textbox_t : public qmenu_textbox_base_t
	{
	public:
		qmenu_jg_textbox_t (char *label, char *textboxtext, int length) : qmenu_textbox_base_t (label, textboxtext, length) {}

		BOOL Key (int key)
		{
			if (key == K_ENTER)
			{
				M_ConfigureNetSubsystem ();

				m_return_state = m_state;
				m_return_onerror = TRUE;
				Key_SetDest (key_game);
				m_state = m_none;
				Cbuf_AddText (va ("connect \"%s\"\n", lanConfig_joinname));

				return FALSE;
			}
			else return this->CommonKey (key, 32, 127);
		}
	};

public:
	qmenu_joingame_t (void)
	{
		// add our options
		this->AddCommonOptions ("Join game");
		this->AddOption (new qmenu_lanconfig_submenu_t ("Search for local games", m_search));
		this->AddOption (new qmenu_jg_textbox_t ("Join game at", lanConfig_joinname, 18));

		// and register our menu
		this->Register (m_joingame);

		// go back one to select the last option as the initial default
		this->Navigate (-1);
	}
} menu_joingame;


class qmenu_startgame_t : public qmenu_lanconfig_t
{
public:
	qmenu_startgame_t (void)
	{
		// add our options
		this->AddCommonOptions ("Start game");
		this->AddOption (new qmenu_lanconfig_submenu_t ("Proceed to Game options", m_gameoptions));

		// and register our menu
		this->Register (m_startgame);

		// go back one to select the last option as the initial default
		this->Navigate (-1);
	}
} menu_startgame;


